/**
 * date:2020/10/22
 * author: zhaojk
 * description:layui 框架扩展
 */
layui.define(function (exports) {
    let request = new function () {
        /**
         * 获取模板页面
         * @param url
         * @param type
         * @param dataParams
         * @param successCall
         * @param async false: 设置为同步, 其他: 异步
         */
        this.getTempForm = function (url, successCall) {
            let ajaxParams = {
                type: "GET",
                url: url,
                dataType: "html",
                success: successCall,
                error: this.ajaxError
            };
            layui.jquery.ajax(ajaxParams);
        }

        /**
         * jquery ajax
         * @param url
         * @param type
         * @param dataParams
         * @param successCall
         * @param async false: 设置为同步, 其他: 异步
         */
        this.ajax = function ajax(url, type, dataParams, successCall, async) {
            let ajaxParams = {
                async: async !== false,
                type: type,
                url: url,
                dataType: "json",
                error: this.ajaxError
            };
            if (!successCall) {
                ajaxParams.success = dataParams;
            } else {
                ajaxParams.success = successCall;
                ajaxParams.data = dataParams;
            }
            layui.jquery.ajax(ajaxParams);
        }

        /**
         * 直接发送json字符串数据
         * @param url
         * @param dataParams
         * @param successCall
         * @param dataType
         * @param async false: 设置为同步, 其他: 异步
         */
        this.bodyAjax = function bodyAjax(url, dataParams, successCall, dataType, async) {
            if ((typeof dataParams) != "string") {
                dataParams = JSON.stringify(dataParams);
            }
            let config = {
                async: async !== false,
                type: "post",
                contentType: "application/json",
                url: url,
                data: dataParams,
                success: successCall,
                error: this.ajaxError
            };
            config["dataType"] = dataType || 'json';
            layui.jquery.ajax(config);
        }

        /**
         * jquery ajax的封装, post方式
         * @param url
         * @param dataParams
         * @param successCall
         * @param async false: 设置为同步, 其他: 异步
         */
        this.postAjax = function postAjax(url, dataParams, successCall, async) {
            this.ajax(url, "POST", dataParams, successCall, async);
        }

        /**
         * jquery ajax的封装, get方式
         * @param url
         * @param dataParams
         * @param successCall
         * @param async false: 设置为同步, 其他: 异步
         */
        this.getAjax = function getAjax(url, dataParams, successCall, async) {
            this.ajax(url, "GET", dataParams, successCall, async);
        }

        /**
         * jquery ajax抛出异常时的回调
         * @param resp
         */
        this.ajaxError = function ajaxError(resp) {
            layui.layer.close(layer.index);
            if (resp.status === 0) {
                layui.layer.alert('服务器可能挂了...请联系开发人员!', {icon: 0});
            } else {
                layui.layer.alert('出现异常: ' + resp.status, {icon: 2});
            }
            console.log(resp);
        }
        /**
         * 文件下载
         * @param api
         * @param params
         * @returns {boolean}
         */
        this.downloadLocalFile = function (api, params) {
            // 创建form表单对象
            let $eleForm = layui.jquery("<form method='post' dataType='html' target='_blank' style='display: none;'></form>");
            // 相关属性赋值
            $eleForm.attr("action", api);

            if (params) {
                layui.jquery.each(params, function (key, value) {
                    let paramInput = layui.jquery("<input />").attr("name", key).val(value); // 其他参数
                    $eleForm.append(paramInput);
                });
            }

            layui.jquery(document.body).append($eleForm);
            $eleForm.submit();
            // 删掉form表单的dom对象
            $eleForm.remove();
        }

        /**
         * POST方式页面跳转
         * @param api
         * @param params
         */
        this.jumpPage = function (api, params) {
            this.downloadLocalFile(api, params);
        }
    };

    exports("request", request);

});

